<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">

<head th:include="heard :: head"></head>

<body>
<nav th:replace="heard :: nav"></nav>
<div class="container-fluid" style="font-size: 14px;">
    <div class="row">
        <div class="col-md-1">
            <nav class="nav flex-column">
                <a class="nav-link" href="#" id="getPD">Definition</a>
                <a class="nav-link" href="#" id="getPV">Value</a>
            </nav>
        </div>
        <div class="col-md-11">
            <div class="card">
                <div class="card-body">
                    <form id="formP">
                        <input id="pid" name="id" type="text" style="display:none" th:value="${pid}">
                        <div class="form-row">
                            <div class="form-group col-md-4">
                                <label for="inputCode">编码</label>
                                <input type="text"  required="required" class="form-control form-control-sm"
                                       id="inputCode" name="code" th:value="${p?.code}"
                                       placeholder=""/></div>
                            <div class="form-group col-md-4">
                                <label for="inputStandardCode">标准编码</label>
                                <input type="text" class="form-control form-control-sm"
                                       id="inputStandardCode" name="standardCode" th:value="${p?.standardCode}"
                                       placeholder=""/></div>

                            <div class="form-group col-md-3">
                                <label for="selectSecurity">安全控制</label>
                                <select class="form-control form-control-sm" id="selectSecurity" name="security"
                                        th:value="${p?.security}">
                                    <option>不可读</option>
                                    <option>可读</option>
                                    <option>可读可增</option>
                                    <option>可读增删</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group col-md-4">
                                <label for="inputName">名称</label>
                                <input type="text" class="form-control form-control-sm"
                                       id="inputName" name="name" th:value="${p?.name}"
                                       placeholder=""/></div>
                            <div class="form-group col-md-4">
                                <label for="inputLocalName">本地名称</label>
                                <input type="text" class="form-control form-control-sm"
                                       id="inputLocalName" name="localName" th:value="${p?.localName}"
                                       placeholder=""/></div>

                            <div class="form-group col-md-3">
                                <label for="selectSortType">排序类型</label>
                                <select class="form-control form-control-sm" id="selectSortType" name="sortType"
                                        th:value="${p?.sortType}">
                                    <option>编码</option>
                                    <option>描述</option>
                                </select></div>
                        </div>
                        <div class="form-row">
                            <div class="form-group col-md-4">
                                <label for="inputDescription">描述</label>
                                <input type="text" class="form-control form-control-sm"
                                       id="inputDescription" name="description" th:value="${p?.description}"
                                       placeholder=""/></div>
                            <div class="form-group col-md-4">
                                <label for="inputExtendedDescription">扩展描述</label>
                                <input type="text" class="form-control form-control-sm"
                                       id="inputExtendedDescription" name="extendedDescription"
                                       th:value="${p?.extendedDescription}"
                                       placeholder=""/></div>

                            <div class="form-group col-md-3">
                                <label for="selectOperatorType">用户操作类型</label>
                                <select class="form-control form-control-sm" id="selectOperatorType"
                                        name="operatorType" th:value="${p?.operatorType}">
                                    <option>用户级</option>
                                    <option>程序级</option>
                                </select></div>
                        </div>
                        <div class="form-row">
                            <div class="form-group col-md-4">
                                <div class="custom-control custom-switch">
                                    <input type="checkbox" class="custom-control-input"
                                           id="inputAutoCache" name="autoCache" th:value="${p?.autoCache}">
                                    <label class="custom-control-label"
                                           for="inputAutoCache">是否自动更新缓存</label>
                                </div>
                            </div>
                            <div class="form-group col-md-4">
                                <div class="custom-control custom-switch">

                                    <input type="checkbox" class="custom-control-input"
                                           id="customSwitch2" name="code1" th:value="${p?.code}">
                                    <label class="custom-control-label"
                                           for="customSwitch2">下拉框中显示编码</label>
                                </div>
                            </div>
                        </div>
                        <div class="form-row form-inline">
                            <label>有效期间</label>
                            <div class="input-group  input-group-sm col-md-6">
                                <input type="date" aria-label="startDate" name="startDate" th:value="${p?.startDate}"
                                       class="form-control">
                                <input type="date" aria-label="endDate" name="endDate" th:value="${p?.endDate}"
                                       class="form-control">
                            </div>
                        </div>
                        <div class="form-group row"></div>
                        <div class="form-group row">
                            <div class="col-sm-10">
                                <button id="saveP" type="button" class="btn   btn-success">保存</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

</div>
</body>

<script>
    var pid = $("#pid").val();
    $(function () {
        $("#saveP").click(function () {
            var fields = $('#formP').serializeArray();
            var obj = {}; //声明一个对象
            $.each(fields, function (index, field) {
                obj[field.name] = field.value; //通过变量，将属性值，属性一起放到对象中
            })
            $.ajax({
                type: "post",
                url: "/parameter",
                async: true,
                contentType: 'application/json',
                dataType: 'JSON',
                data: JSON.stringify(obj),//将对象转为json字符串
                success: function (obj) {
                    let id = obj.data;
                    $("#pid").val(id);
                    alert("保存成功");
                    window.location.href = '/home/p/' + obj.data + '/edit';
                },
                error: function (obj) {
                    alert(obj.msg);
                }
            });
        })


        $("#getPD").click(function () {
            if (pid === "") {
                alert("请先保存参数");
                return;
            }
            window.location.href = '/home/p/' + pid + '/pd';

        })
        $("#getPV").click(function () {
            if (pid === "") {
                alert("请先保存参数");
                return;
            }
            window.location.href = '/home/p/' + pid + '/v';

        })
    });
</script>
</html>
